//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.custom.scrollview;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

public class CScrollView extends ScrollView {
    private View inner;
    private float y;
    private boolean isAnim = false;
    private Rect normal = new Rect();
    private CScrollView.OnBorderListener onBorderListener;

    public CScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onFinishInflate() {
        if(this.getChildCount() > 0) {
            this.inner = this.getChildAt(0);
        }

    }

    public boolean onTouchEvent(MotionEvent ev) {
        if(this.inner == null) {
            return super.onTouchEvent(ev);
        } else {
            if(this.isAnim) {
                this.commOnTouchEvent(ev);
            }

            return super.onTouchEvent(ev);
        }
    }

    public void setIsAnim(boolean isAnim) {
        this.isAnim = isAnim;
    }

    private void commOnTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch(action) {
            case 0:
                this.y = ev.getY();
                break;
            case 1:
                if(this.isNeedAnimation()) {
                    this.animation();
                }
                break;
            case 2:
                float preY = this.y;
                float nowY = ev.getY();
                int deltaY = (int)(preY - nowY);
                this.scrollBy(0, deltaY);
                this.y = nowY;
                if(this.isNeedMove()) {
                    if(this.normal.isEmpty()) {
                        this.normal.set(this.inner.getLeft(), this.inner.getTop(), this.inner.getRight(), this.inner.getBottom());
                    }

                    this.inner.layout(this.inner.getLeft(), this.inner.getTop() - deltaY, this.inner.getRight(), this.inner.getBottom() - deltaY);
                }
        }

    }

    private void animation() {
        TranslateAnimation ta = new TranslateAnimation(0.0F, 0.0F, (float)this.inner.getTop(), (float)this.normal.top);
        ta.setDuration(200L);
        this.inner.startAnimation(ta);
        this.inner.layout(this.normal.left, this.normal.top, this.normal.right, this.normal.bottom);
        this.normal.setEmpty();
    }

    private boolean isNeedAnimation() {
        return !this.normal.isEmpty();
    }

    private boolean isNeedMove() {
        int offset = this.inner.getMeasuredHeight() - this.getHeight();
        int scrollY = this.getScrollY();
        return scrollY == 0 || scrollY == offset;
    }

    public void setOnBorderListener(CScrollView.OnBorderListener onBorderListener) {
        this.onBorderListener = onBorderListener;
        if(onBorderListener != null) {
            ;
        }
    }

    private void doOnBorderListener() {
        if(this.inner != null && this.inner.getMeasuredHeight() <= this.getScrollY() + this.getHeight()) {
            if(this.onBorderListener != null) {
                this.onBorderListener.onBottom();
            }
        } else if(this.getScrollY() == 0 && this.onBorderListener != null) {
            this.onBorderListener.onTop();
        }

    }

    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        this.doOnBorderListener();
    }

    public interface OnBorderListener {
        void onBottom();

        void onTop();
    }
}
